package 数据结构.队列.链表队列;

import 数据结构.队列.Queue;

/**
 * @author Veng Su 1344114844@qq.com
 * @date 2018/7/20 15:49
 */
public class LinkedListQueue<E> implements Queue<E> {
    private class Node {
        public E e;
        public Node next;

        public Node(E e, Node next) {
            this.e = e;
            this.next = next;
        }

        public Node(E e) {
            this(e, null);
        }

        @Override
        public String toString() {
            return "Node{" +
                    "e=" + e +
                    '}';
        }
    }
    private Node head,tail;
    private int size;

    @Override
    public void enQueue(E e) {
        if (tail==null){
            tail=new Node(e);
            head=tail;
        }else {
            tail.next=new Node(e);
            tail=tail.next;
        }
        size++;
    }

    @Override
    public E deQueue() {
        if (isEmpty()){
            throw new IllegalArgumentException("队列为空");
        }
        Node ret=head;
        head=head.next;
        if (head==null){
            tail=null;
        }
        size--;
        return ret.e;
    }

    @Override
    public E getFront() {
        if (isEmpty()){
            throw new IllegalArgumentException("队列为空");
        }
        return head.e;
    }

    @Override
    public int getSize() {
        return size;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public String toString() {
        StringBuilder res=new StringBuilder();
        res.append("LinkedListQueue: head ");
        Node cur=head;
        while (cur!=null){
            res.append(cur).append("->");
            cur=cur.next;
        }
        res.append("null tail ");
        return res.toString();
    }
}
